package com.assessment.controller;

import com.assessment.common.api.Result;
import com.assessment.model.dto.AssessmentRecordDTO;
import com.assessment.model.entity.AssessmentPeriod;
import com.assessment.model.vo.AssessmentResultVO;
import com.assessment.model.vo.AssessmentTaskVO;
import com.assessment.service.AssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 评测控制器
 */
@RestController
@RequestMapping("/api/assessment")
public class AssessmentController {

    @Autowired
    private AssessmentService assessmentService;

    /**
     * 获取当前评测周期
     */
    @GetMapping("/currentPeriod")
    public Result<AssessmentPeriod> getCurrentPeriod() {
        AssessmentPeriod period = assessmentService.getCurrentPeriod();
        return Result.success(period);
    }

    /**
     * 获取教师的评测任务
     */
    @GetMapping("/tasks")
    public Result<List<AssessmentTaskVO>> getTeacherTasks(@RequestParam Integer periodId,
                                                          HttpServletRequest request) {
        Integer teacherId = getTeacherId(request);
        List<AssessmentTaskVO> tasks = assessmentService.getTeacherTasks(teacherId, periodId);
        return Result.success(tasks);
    }

    /**
     * 获取评测任务详情
     */
    @GetMapping("/task/{relationId}")
    public Result<AssessmentTaskVO> getTaskDetail(@PathVariable Integer relationId) {
        AssessmentTaskVO task = assessmentService.getTaskDetail(relationId);
        return Result.success(task);
    }

    /**
     * 提交评测记录
     */
    @PostMapping("/submit")
    public Result<Boolean> submitAssessment(
            @RequestBody AssessmentRecordDTO recordDTO,
            HttpServletRequest request) {
        Integer teacherId = getTeacherId(request);
        boolean success = assessmentService.submitAssessment(recordDTO, teacherId);
        return Result.success(success);
    }

    /**
     * 获取教师的评测结果
     */
    @GetMapping("/result/teacher")
    public Result<AssessmentResultVO> getTeacherResult(@RequestParam Integer periodId,
                                                       HttpServletRequest request) {
        Integer teacherId = getTeacherId(request);
        AssessmentResultVO result = assessmentService.getTeacherResult(teacherId, periodId);
        return Result.success(result);
    }

    /**
     * 获取部门的评测结果（管理员）
     */
    @GetMapping("/result/department")
    public Result<List<AssessmentResultVO>> getDepartmentResults(@RequestParam Integer departmentId,
                                                                 @RequestParam Integer periodId) {
        List<AssessmentResultVO> results = assessmentService.getDepartmentResults(departmentId, periodId);
        return Result.success(results);
    }

    /**
     * 获取所有教师的评测结果（管理员）
     */
    @GetMapping("/results")
    public Result<List<AssessmentResultVO>> getAllResults(@RequestParam Integer periodId) {
        List<AssessmentResultVO> results = assessmentService.getAllResults(periodId);
        return Result.success(results);
    }

    /**
     * 从请求中获取教师ID
     */
    private Integer getTeacherId(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从JWT中获取用户ID，实际应用中应查询当前用户的教师ID
//        Integer userId = JwtUtil.getUserId(token);

        // TODO: 实际项目中需要查询当前用户关联的教师ID
        // 这里简化处理，直接返回1，代表张三
        return 1;
    }
}
